/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myconfession.utils;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.Comment;
import com.restfb.types.FacebookType;
import com.restfb.types.Page;
import com.restfb.types.Post;
import com.restfb.types.User;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;
import myconfession.dto.facebookInfo.Commenter;
import myconfession.dto.facebookInfo.FacebookInfo;

/**
 *
 * @author Liem
 */
public class FacebookUtil {

    private FacebookUtil() {
    }

    public static String getFbName(String access_token) {
        FacebookClient facebookClient = new DefaultFacebookClient(access_token);
        User user = facebookClient.fetchObject("me", User.class);
        return user.getName();
    }

    public static String postToPage(String pageId, String content, String access_token, String hashtag)
            throws com.restfb.exception.FacebookOAuthException {
        FacebookClient facebookClient = new DefaultFacebookClient(access_token);
        if (hashtag != null && hashtag.length() > 0) {
            content = hashtag + "\n\n" + content;
        }
        FacebookType publishMessageResponse = facebookClient.publish(pageId + "/feed", FacebookType.class, Parameter.with("message", content));
        String postId = publishMessageResponse.getId();
        return postId;
    }

    public static boolean removePostOn(String postId, String access_token) {
        FacebookClient facebookClient = new DefaultFacebookClient(access_token);
        boolean deleted = facebookClient.deleteObject(postId);
        return deleted;
    }

    public static String getLongLiveToken(String access_token) {
        String appId = ConstantProvider.getInstance().getFbAppId();
        String appSecret = ConstantProvider.getInstance().getFbAppSecret();
        FacebookClient facebookClient = new DefaultFacebookClient(access_token);
        FacebookClient.AccessToken token = facebookClient.obtainExtendedAccessToken(appId, appSecret, access_token);
        return token.getAccessToken();
    }

    public static long getPostLikeCount(String objectId, String access_token) {
        FacebookClient facebookClient = new DefaultFacebookClient(access_token);
        Post post = facebookClient.fetchObject(objectId, Post.class);
        long count = post.getLikes().getData().size();
        //Connection<Post> posts = facebookClient.fetchConnection("a3xconfession/feed", Post.class);
        return count;
    }

    public static long getComment(String objectId, String access_token) {
        FacebookClient facebookClient = new DefaultFacebookClient(access_token);
        Post post = facebookClient.fetchObject(objectId, Post.class);
        Post.Comments comments = post.getComments();
        String a = comments.getData().get(0).getFrom().getName();
        System.out.println(a);
        return 0;
    }

    public static Connection<Post> getAllPost(String pageId, FacebookClient facebookClient) {
        Connection<Post> posts = facebookClient.fetchConnection(pageId + "/feed", Post.class);
        return posts;
    }

    public static String printFBInfo(String pageId, String access_token) {
        try {

            FacebookClient facebookClient = new DefaultFacebookClient(access_token);
            Page page = facebookClient.fetchObject(pageId, Page.class);
            FacebookInfo facebookInfo = new FacebookInfo();
            facebookInfo.setFbPageId(pageId);
            facebookInfo.setPageAbout(page.getAbout());
            if (page.getLikes() != null) {
                facebookInfo.setLikeCount(page.getLikes().intValue());
            }
            Connection<Post> posts = getAllPost(pageId, facebookClient);
            facebookInfo.setTotalPost(posts.getData().size());
            int commentCount = 0;
            Hashtable<String, Integer> userComment = new Hashtable<String, Integer>();
            for (int i = 0; i < posts.getData().size(); i++) {
                Post post = posts.getData().get(i);
                if (post.getComments() != null) {
                    if (post.getComments().getData() != null) {
                        for (int j = 0; j < post.getComments().getData().size(); j++) {
                            commentCount++;
                            Comment comment = post.getComments().getData().get(j);
                            String commenterName = comment.getFrom().getName();
                            if (userComment.containsKey(commenterName)) {
                                userComment.put(commenterName, userComment.get(commenterName) + 1);
                            } else {
                                userComment.put(commenterName, 1);
                            }
                        }
                    }
                }
            }
            facebookInfo.setTotalComment(commentCount);
            Enumeration<String> keys = userComment.keys();
            while (keys.hasMoreElements()) {
                String commenterName = keys.nextElement();
                Commenter commenter = new Commenter();
                commenter.setCommenterName(commenterName);
                commenter.setCommentCount(userComment.get(commenterName));
                facebookInfo.getCommenter().add(commenter);
            }

            try {
                StringWriter sw = new StringWriter();
                XMLUtility.marshallJAXB(sw, facebookInfo);
                return sw.toString();
            } catch (JAXBException ex) {
                Logger.getLogger(FacebookUtil.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (Exception e) {
            return "error";
        }
        return null;
    }
}
